package com.xzqwjw.taskmanager.fragment;

import java.util.List;
import java.util.Map;

import com.leaderinfo.leaderinfojxyz.R;
import com.leaderinfo.model.CategoryUnit;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class ShowListFragment extends Fragment {

	private String strDBFile = "";
	private ProgressDialog objPD;

	List<Map<String, Object>> listLeader = null;

	int intUnitID = 0;
	String strUnitID = "0";

	int intPrincipalNumber = 0;
	int intDeputyNumber = 0;
	int intDepth = 0;

	private WebView objWV;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent objIT = getActivity().getIntent();
		strDBFile = objIT.getStringExtra("DBFilePath");

		Bundle objBundle = getArguments();

		if (objBundle != null) {
			strUnitID = objBundle.getString("UnitID");
			new AsyncLoadDataList().execute(strUnitID);
		} else {
			strUnitID = "0";
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_showlist, container,
				false);
		if (view != null) {
			objWV = (WebView) view.findViewById(R.id.wvShowList);
		}
		return view;
	}

	class AsyncLoadDataList extends android.os.AsyncTask<String, Void, Void> {

		@Override
		protected void onPreExecute() {
			objPD = ProgressDialog.show(getActivity(), "读取数据",
					"正在读取机构人员数据，请稍等！", true, false);
		}

		@Override
		protected Void doInBackground(String... params) {
			intUnitID = Integer.parseInt(params[0]);
			if (intUnitID > 0) {
				com.leaderinfo.dao.PartyInfoDao objPartyInfoDAO = new com.leaderinfo.dao.PartyInfoDao(strDBFile);
				listLeader = objPartyInfoDAO.getLeadersByUnitID(intUnitID);

				CategoryUnit objUnitCategory = getUnitCategory(intUnitID);
				if (objUnitCategory != null) {
					intPrincipalNumber = objUnitCategory.PrincipalNumber;
					intDeputyNumber = objUnitCategory.DeputyNumber;
					intDepth = objUnitCategory.Depth;
				}
			}
			return null;
		}


		@SuppressLint("SetJavaScriptEnabled")
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);

			// 载入这个html页面
			WebSettings webSettings = objWV.getSettings();
			// 设置WebView属性，能够执行Javascript脚本
			webSettings.setJavaScriptEnabled(true);
			webSettings.setDefaultTextEncodingName("UTF-8");
			objWV.addJavascriptInterface(new JsInteration(), "control");
			objWV.setWebChromeClient(new WebChromeClient() {
			});
			objWV.setBackgroundColor(0);

			String strBaseUrl = "file:///mnt/sdcard/PartyInfo/";
			// 加载需要显示的网页
			String strHtml = getHtmlByUnitID();
			objWV.loadDataWithBaseURL(strBaseUrl, strHtml, "text/html",
					"UTF-8", null);

			objPD.dismiss();
		}

		// 根据UnitID得到此UnitID的相关信息
		private CategoryUnit getUnitCategory(int intUnitID) {
			com.leaderinfo.dao.UnitCategoryDao objUnitCategoryDAO = new com.leaderinfo.dao.UnitCategoryDao(strDBFile);
			Map<String, Object> mapUnitCategory = objUnitCategoryDAO
					.getRecordByAID(intUnitID);
			if (mapUnitCategory != null) {
				return (CategoryUnit) mapUnitCategory.get("UnitCategory");
			} else {
				return null;
			}
		}
			
	}

	public class JsInteration {
		@JavascriptInterface
		public void showInfoWV(int intBasicID) {
			Intent objIT = new Intent(getActivity().getApplicationContext(),
					ActivityShowInfo.class);
			objIT.putExtra("AID", intBasicID + "");
			objIT.putExtra("DBFilePath", strDBFile);
			startActivity(objIT);
		}
	}

	private String getHtmlByUnitID() {
		String strHtml = "";
		String strJS = "<script type=\"text/javascript\">"
				+ "function showInfoJS(intBasicID) {"
				+ "window.control.showInfoWV(intBasicID)" + "}" + "</script>";

		String strHtmlHead = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\""
				+ "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
				+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
				+ "<head>"
				+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
				+ "<title>根据单位编号查询得到的单位人员列表</title>"
				+ "<style type=\"text/css\">"
				+ "* { margin:0; padding:0;color:#FFFFFF; font-family:Verdana, Geneva, sans-serif; }"
				+ "table { border-top:1px solid #507324; border-right:1px solid #507324; }"
				+ "td { border-bottom:1px solid #507324; border-left:1px solid #507324;"
				+ "padding:3px; font-size:14px; text-align:center; }"
				+ "h1 { margin:10px; text-align:center; font-size:18px; }"
				+ ".atitle{background:#507324; color:#FFF; }"
				+ ".atab td{text-align:center; background:#CFE7B3; color:#000; font-size:16px; }"
				+ "</style>" + strJS + "</head>" + "<body>";

		String strHtmlFoot = "</body></html>";

		strHtml = strHtmlHead;
		strHtml += setHtmlLeader();
		// if (intDepth > 1) {
		// strHtml += setHtmlLeader(listLeaderMain, "领导班子正职", 1);
		// strHtml += setHtmlLeader(listLeaderVia, "领导班子副职", 2);
		// strHtml += setHtmlLeader(listLeaderOther, "其他成员", 0);
		// } else {
		// strHtml += setHtmlLeader(listLeaderMain, "领导班子正职", 1);
		// strHtml += setHtmlLeader(listLeaderVia, "领导班子副职", 2);
		// }
		strHtml += strHtmlFoot;

		strHtml = strHtml.replaceAll("\r", "<br />");
		strHtml = strHtml.replaceAll("\n", "<br />");
		strHtml = strHtml.replaceAll("\r\n", "<br />");
		return strHtml;
	}

	// 设置领导列表的html代码
	private String setHtmlLeader() {
		String strHtml = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
				+ "<tr><td colspan=\"7\" class=\"atitle\"><h1>单位人员花名册</h1></td></tr>"
				+ "<tr class=\"atab\">"
				+ "<td>职务</td><td>头像</td><td>姓名</td><td>性别</td><td>出生年月（年龄）</td><td>籍贯</td><td>收藏</td>"
				+ "</tr>";
		if (listLeader != null) {
			for (int i = 0; i < listLeader.size(); i++) {
				strHtml += setTR(listLeader.get(i));
			}
		}
		strHtml += "</table>";
		return strHtml;
	}

	private String setTR(Map<String, Object> mapInfo) {
		int intAID = 0;
		String strJobName = "&nbsp;";
		String strHeadPhotoPath = "&nbsp;";
		String strRealName = "&nbsp;";
		String strSex = "&nbsp;";
		String strBirthdayAge = "&nbsp;";
		String strBirthPlace = "&nbsp;";
		int intIsFavorite = 0;
		String strIsFavorite = "";

		intAID = (Integer) mapInfo.get("BasicID");
		strJobName = (String) mapInfo.get("JobName");
		strHeadPhotoPath = (String) mapInfo.get("HeadPhotoPath");
		strRealName = (String) mapInfo.get("RealName");
		strSex = (String) mapInfo.get("Sex");
		strBirthdayAge = (String) mapInfo.get("BirthdayAge");
		strBirthPlace = (String) mapInfo.get("BirthPlace");
		intIsFavorite = (Integer) mapInfo.get("IsFavorite");
		if (intIsFavorite == 0)
			strIsFavorite = "否";
		else
			strIsFavorite = "是";

		String strAID = intAID + "";
		strJobName = (strJobName == "") ? "&nbsp;" : strJobName;
		strHeadPhotoPath = (strHeadPhotoPath == "") ? "&nbsp;"
				: strHeadPhotoPath;
		strRealName = (strRealName == "") ? "&nbsp;" : strRealName;
		strSex = (strSex == "") ? "&nbsp;" : strSex;
		strBirthdayAge = (strBirthdayAge == "") ? "&nbsp;" : strBirthdayAge;
		strBirthPlace = (strBirthPlace == "") ? "&nbsp;" : strBirthPlace;
		// strIsFavorite = (strIsFavorite == "") ? "&nbsp;" : strIsFavorite;

		String strHtml = "<tr><td>" + strJobName + "</td>"
				+ "<td><a href=\"javascript:void(0)\" onclick=\"showInfoJS("
				+ strAID + ")\"><img width=\"64\" height=\"80\" src=\""
				+ strHeadPhotoPath + "\" /></a></td><td>" + strRealName
				+ "</td><td>" + strSex + "</td><td>" + strBirthdayAge
				+ "</td><td>" + strBirthPlace + "</td><td>" + strIsFavorite
				+ "</td></tr>";
		return strHtml;
	}

}
